package com.dyd.smco.controller;

import com.dyd.smco.core.annotation.JsonBody;
import com.dyd.smco.core.utils.response.ResponseUtil;
import com.dyd.smco.db.domain.SmartCommunityWelcome;
import com.dyd.smco.model.SmartCommunityWelcomeBody;
import com.dyd.smco.service.SmartCommunityWelcomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author ：duyd@segimail.com
 * @class ：com.dyd.smco.core.controller.WelcomeContoller
 * @date ：Created in 2025/8/7 15:05
 * @description：欢迎广告位
 * @modified By：
 * @version:
 */
@Slf4j
@RestController
@RequestMapping("/smart/welcome")
@Validated
public class SmartCommunityWelcomeContoller {

    @Autowired
    private SmartCommunityWelcomeService welcomeService;

    /**
     * 查询
     */
    @GetMapping("/list")
    public Object list(SmartCommunityWelcomeBody body) {
        return ResponseUtil.okList(welcomeService.querySelective(body));
    }

    /**
     * 添加
     */
    @PostMapping("/create")
    public Object create(@Valid @RequestBody SmartCommunityWelcome welcome) {
        Object error = welcomeService.validate(welcome);
        if (error != null) {
            return error;
        }
        if (welcomeService.add(welcome) == 0){
            return ResponseUtil.addDataFailed();
        }
        return ResponseUtil.ok(welcome);
    }

    /**
     * 详情
     */
    @GetMapping("/read")
    public Object read(@JsonBody String id) {
        return ResponseUtil.ok(welcomeService.findById(id));
    }

    /**
     * 编辑
     */
    @PostMapping("/update")
    public Object update(@Valid @RequestBody SmartCommunityWelcome ad) {
        Object error = welcomeService.validate(ad);
        if (error != null) {
            return error;
        }
        if (welcomeService.updateVersionSelective(ad) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(ad);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Object delete(@JsonBody String id) {
        if (welcomeService.deleteById(id) == 0){
            return ResponseUtil.deletedDataFailed();
        }
        return ResponseUtil.ok();
    }

}
